<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use App\Admin\Models\ContentModel;

class InformationController extends AdminController
{
    /**
     * @var int 记录ID
     */
    private $id = 1;

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '招聘联系方式';

    /**
     * Set description for following 4 action pages.
     *
     * @var array
     */
    protected $description = [
        'edit' => '请在此处编辑招聘联系方式',
    ];

    /**
     * Edit interface.
     *
     * @param Content $content
     *
     * @return Content|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function detail(Content $content)
    {
        if (\Request::isMethod('put')) {
            return $this->save();
        }

        return $content
            ->title($this->title())
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->body($this->form()->edit($this->id));
    }

    /**
     * save
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    private function save()
    {
        $this->store();

        admin_toastr('保存成功');
        return redirect('/admin/information');
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(ContentModel::find($this->id));

        $form->text('title_zh', __('联系人姓名-中文'))->setWidth(4, 2)->rules('required|min:1|max:20')->maxlength(20)
            ->placeholder('输入联系人姓名-中文，字符长度限制在20字符内')->required();
        $form->text('title_en', __('联系人姓名-英文'))->setWidth(4, 2)->rules('required|min:1|max:40')->maxlength(40)
            ->placeholder('输入联系人姓名-英文，字符长度限制在40字符内')->required();
        $form->email('introduction_zh', __('联系邮箱'))->setWidth(4, 2)->rules('required|min:1|max:40')->maxlength(40)
            ->placeholder('输入联系邮箱，字符长度限制在40字符内')->required();
        $form->text('phone', __('联系电话'))->rules('nullable|min:1|max:40')->maxlength(40)
            ->placeholder('输入联系电话，字符长度限制在40字符内')->setWidth(4, 2);
        $form->text('content_zh', __('文案说明-中文'))->setWidth(6, 2)->rules('nullable|min:1|max:200')->maxlength(200)
            ->placeholder('输入文案说明-中文，字符长度限制在200字符内');
        $form->text('content_en', __('文案说明-英文'))->setWidth(6, 2)->rules('nullable|min:1|max:800')->maxlength(800)
            ->placeholder('输入文案说明-英文，字符长度限制在800字符内');
        $form->cropper('image_url', __('图片说明'))->cRatio(409, 88)->setWidth(6, 2)
            ->help('图片推荐尺寸：409*88，已固定宽高比');

        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();

            // 去掉`删除`按钮
            $tools->disableDelete();

            // 去掉`查看`按钮
            $tools->disableView();

        });

        $form->footer(function (Form\Footer $footer) {
            // 去掉`重置`按钮
            $footer->disableReset();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        // 设置表单提交的action
        $form->setAction('information');

        // 设置按钮样式
        \Encore\Admin\Admin::style('.btn-group {float: left!important;} .fa-info-circle {margin-left:10px;}');

        return $form;
    }
}
